using System; using System.Collections.Generic; using UnityEngine; using wildlogicgames; namespace RebelHeartEclipse { public class CharacterActionStage : MonoBehaviour { [Header("--------------------CAMERA MANAGER PLUGIN")] [Header("Plugin the camera manager manager here.")] [SerializeField] public CameraController _cameraController; [Header("--------------------UI MANAGER PLUGIN")] [Header("Plugin the UI manager here.")] [SerializeField] public UIManager _UIManager; [Header("--------------------SQUAD MANAGER PLUGIN")] [Header("Plugin the squad manager here.")] [SerializeField] public SquadManager _squadManager; [Header("--------------------BATTLE GROUND GRIDGEN PLUGIN")] [Header("Plugin the grid generator here.")] [SerializeField] public BattleGroundGridGenerator _battleGroundGridGenerator; private Dictionary<(int x, int y), (int px, int py)> _pathFindingMovement; public Dictionary<(int x, int y), (int px, int py)> PathFindingMovement { get { return _pathFindingMovement; } set { _pathFindingMovement = value; } } private Queue _currentMovePath = new Queue(); private Transform _transform; private const float _moveSpeed = 1.0f; private Vector3 _targetMovePosition; private GridCellObj _gridCellObj; private CharacterObj _characterObj, _targetCharacterObj; private int _x, _y, _clickedX, _clickedY; private GameObject[] _timerObj = new GameObject[4]; private WTimer[] _timer = new WTimer[4]; private float _waitime = 0.1f;//0.25f; private bool _process; private void Awake() => Setup(); private void Setup() { _transform = this.transform; _pathFindingMovement = new Dictionary<(int, int), (int, int)>(); _process = true; SetupTimers(); } private void SetupTimers() { for (int i = 0; i < _timerObj.Length; i++) { _timerObj[i] = new GameObject(); _timerObj[i].AddComponent(); _timer[i] = _timerObj[i].GetComponent(); _timer[i].Reset(); //_timer[i].StartTimer(_waitime); } } private void Update() { if (!_process) return; UpdateInputLockTimer(0); UpdateCharacterMovement(); UpdateCharacterAttack(); } private void UpdateCharacterMovement() { if (EventManager.GetEventFlag() == EventFlagID.EventFlag_Process_Player_Move) { _characterObj.transform.position = Vector3.MoveTowards(_characterObj.transform.position, _targetMovePosition, _moveSpeed * Time.deltaTime); _transform.LookAt(_characterObj.transform.position); if (Vector3.Distance(_characterObj.transform.position, _targetMovePosition) < 0.01f) { _characterObj.transform.position = _targetMovePosition; if (_currentMovePath.Count > 0) { // More steps to walk _targetMovePosition = _currentMovePath.Dequeue(); _targetMovePosition.y += _characterObj.GetHeightDifference(); } else { // Finished movement _cameraController.SetPivotPoint(_targetMovePosition); EventManager.SetEventFlag(EventFlagID.EventFlag_Completed_Player_Move); } } } if (EventManager.GetEventFlag() == EventFlagID.EventFlag_Completed_Player_Move) { int x = EventManager.GetGridCellClicked_X(); int y = EventManager.GetGridCellClicked_Y(); _characterObj.DecreaseMovePtsCount(); _characterObj.SetState(CharacterStateID.Idle); _battleGroundGridGenerator._baseGridCellObjCached[x, y].AssignCharacterObj(_characterObj, _targetMovePosition); _battleGroundGridGenerator._baseGridCellObjCached[_x, _y].RemoveCharacterObj(); _cameraController.ApplyIsomViewType(_clickedX, _clickedY, CameraBehaviour.CircleAroundPivot); EventManager.SetEventFlag(EventFlagID.EventFlag_Idle); } } public void SetMoveTarget((int x, int y) target, (int x, int y) start) { //*Omitted } private List BuildPath((int x, int y) target, (int x, int y) start) { //*Omitted return path; } private void UpdateCharacterAttack() { if (EventManager.GetEventFlag() == EventFlagID.EventFlag_Idle_Player_Attack) { _cameraController.ApplyCamFilter(0); } if (EventManager.GetEventFlag() == EventFlagID.EventFlag_Process_Player_Attack) { if (_characterObj.InitAttackFX())//; { _cameraController.DisableCameraJounreyLock(); int x = EventManager.GetCurrentPlayerCellX(); int y = EventManager.GetCurrentPlayerCellY(); Vector3 lookAtPos = _battleGroundGridGenerator.GetSpecifiedCellPosition(x, y); _cameraController.SetPivotPoint(lookAtPos); _cameraController.transform.LookAt(lookAtPos); _cameraController.SetBehaviour(CameraBehaviour.IsometricToCharacter); _cameraController.ApplyCamFilter(1);//_cameraController.ResetCamFilterUpdate(); } } if (EventManager.GetEventFlag() == EventFlagID.EventFlag_Update_UI_Status) { UpdateUIStatusForMeleePlayerAction(); UpdateUIStatusForRangedPlayerAction(); } if (EventManager.GetEventFlag() == EventFlagID.EventFlag_Completed_Player_Attack) { if (_timer[2].HasTimerFinished(true)) { _cameraController.DisableCameraJounreyLock();//_camJourneyLocked = false; _cameraController.EnabledCameraBehaviour();//_processCamBehaviour = true; EventManager.SetEventFlag(EventFlagID.EventFlag_Idle); _UIManager.DisableBattleUIWindow(); _cameraController.LockedIsometricViewUpdate(); _cameraController.SetBehaviour(CameraBehaviour.CircleAroundPivot); _timer[1].Reset(); _timer[2].Reset(); return; } } return; } private bool IsQualifiedUIStatusForMelee() { if (_characterObj == null) return false; if (_characterObj.GetCharacterStat()._characterClass == CharacterClass.Bowman && _characterObj.IsPlayerCharacter()) return false; return true; } public void UpdateUIStatusForMeleePlayerAction() { if (!IsQualifiedUIStatusForMelee()) return; if (_timer[1].HasTimerFinished(true)) { _UIManager.EnableBattleUIWindow(); _UIManager.DisableProceedBattleUIWindow(); _characterObj = _squadManager.GetCurrentEnemyObj(); int playerDmg = _squadManager.GetCurrentPlayerObj().GetCharacterStat()._atkPointTotal; _characterObj.DeductHP(playerDmg); _characterObj.SetState(CharacterStateID.Damaged); _characterObj = _UIManager.InitEnemyBattleUI(); _UIManager.DisablePlayerBattleUIWindow(); _cameraController.SetBehaviour(CameraBehaviour.IsometricToCharacter); _timer[2].Reset(); _timer[2].StartTimer(0.5f); _squadManager.GetCurrentEnemyObj().SetState(CharacterStateID.Damaged); _squadManager.GetCurrentEnemyObj().InitDamagedFX(); } if (_timer[2].HasTimerFinished(true)) { _characterObj.SetState(CharacterStateID.Idle); _squadManager.GetCurrentEnemyObj().SetState(CharacterStateID.Damaged); _cameraController.DisableCameraBehaviour();//_processCamBehaviour = false; } if (_UIManager.ProcessEnemyBattleUIUpdate()) { EventManager.SetEventFlag(EventFlagID.EventFlag_Completed_Player_Attack); InitTimer(2, 1.0f); return; } } public void UpdateUIStatusForRangedPlayerAction() { //*Omitted. } public void ProcessGridToViewClickReport(int clickX, int clickY)//, ref GridCellObj gridCellObj) { _clickedX = clickX; _clickedY = clickY; _gridCellObj = _battleGroundGridGenerator.GetGridCellObj(_clickedX, _clickedY);//gridCellObj; if (_gridCellObj == null) return; EventManager.SetGridCellIDClicked(clickX, clickY); if (ProcessFirstClickReport()) return; if (ProcessMoveableClickReport(clickX, clickY)) return; if (ProcessAttackableClickReport(clickX, clickY)) return; ProcessCharacterClickReport(clickX, clickY); ProcessEmptyGridCellClickReport(clickX, clickY); _gridCellObj.CellOnClick(_cameraController.GetPivotPoint()); } private bool ProcessFirstClickReport() { //*Omitted return false; } private bool ProcessMoveableClickReport(int clickX, int clickY) { //*Omitted return false; } private bool ProcessAttackableClickReport(int clickX, int clickY) { //*Omitted return false; } private void ProcessCharacterClickReport(int clickX, int clickY) { //*Omitted return; } private void ProcessEmptyGridCellClickReport(int clickX, int clickY) { if (!_gridCellObj.IsCharacterOnCell()) { _cameraController.SetPivotPoint(_battleGroundGridGenerator.GetCenterGridPosition()); _cameraController.ApplyIsomViewType(clickX, clickY, CameraBehaviour.CircleAroundPivot); if (_characterObj != null) _battleGroundGridGenerator.DeactivateMoveableCells(_x, _y, _characterObj.GetCharacterStat()._characterClass); } return; } private void UpdateInputLockTimer(int timerId) { //This provides few frames between state changes when we click the mouse. if (!_cameraController.IsInputLocked()) return; if (_timer[timerId].HasTimerFinished(true)) { _timer[timerId].Reset(); _cameraController.DisableInputLock();// = false; } } private void InitTimer(int timerId, float waitTime) { _timer[timerId].Reset(); _waitime = waitTime; _timer[timerId].StartTimer(_waitime); } //Use SquadManager.cs for Enemy & Player CharacterObj references. //public void AssignCharacterObject(CharacterObj characterObj) => _characterObj = characterObj; //public void AssignTargetCharacterObject(CharacterObj characterObj) => _targetCharacterObj = characterObj; } }